
var data = [
    {id: 1, name: "安徽", key: 1},
    {id: 2, name: "江苏", key: 2},
    {id: 3, name: "合肥", pid: 1, key: 3},
    {id: 4, name: "庐阳区", pid: 3, key: 4},
    {id: 5, name: "大杨镇", pid: 4, key: 5},
    {id: 6, name: "南京", pid: 2, key: 6},
    {id: 7, name: "玄武区", pid: 6, key: 7},
    {id: 8, name: "梅园新村街道", pid: 7, key: 8},
    {id: 9, name: "上海", key: 9},
    {id: 10, name: "黄浦区", pid: 9, key: 10},
    {id: 11, name: "外滩", pid: 10, key: 11},
    {id: 12, name: "安庆", pid: 1, key: 12}
];

function sonsTree(obj, arr) {
    var children = new Array();
    for (var i = 0; i < arr.length; i++) {
        if (arr[i].pid == obj.id) {  //pid等于加入数组
            sonsTree(arr[i], arr);//递归出子元素
            arr[i].pname=obj.name;
            children.push(arr[i]);
        }
    }
    if (children.length > 0) {
        obj.children = children;
    }
    return obj;
}


function treeUtils() {
    let ptree = [];
    let tree = [];
    for (var i = 0; i < data.length; i++) {
        if (data[i].pid == null) {//获取父元素
            let o = sonsTree(data[i], data);
            ptree.push(o);
        }
    }
    console.info(ptree);
    return ptree;
}

treeUtils()
